package robotAPI;

import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import clientAPI.Turn;
import clientAPI.Drive;

public class RobotRequestMessage {
	private int move;
	private int rotate;
	private int numScans;
	private boolean reboot;
	private boolean automatic;
	private Turn T;
	private Drive D;
	
	
	public RobotRequestMessage(int move, int rotate, int numScans, boolean reboot) {
		this.move = move;
		this.rotate = rotate;
		this.reboot = reboot;
		this.numScans = numScans;
		automatic = false;
		T = null; D = null;
	}

	public boolean automatic() {
		return automatic;
	}
		
	public RobotRequestMessage(Drive drive, Turn t){
		T = t;
		D = drive;
		move = 0; rotate = 0; numScans = 0; reboot= false;
		automatic = false;
	}
	
	public RobotRequestMessage(boolean aut){
		T = null;
		D = null;
		move = 0; rotate = 0; numScans = 0; reboot= false;
		automatic = aut;
	}
	
	public String getXML() {

		try {
			DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
			DocumentBuilder build = dFact.newDocumentBuilder();
			Document doc = build.newDocument();
			Element root = doc.createElement("s2r");
			doc.appendChild(root);

			if (this.rotate != 0) {
				Element rotateField = doc.createElement("rotate");
				rotateField.setAttribute("req", Integer.toString(this.rotate));
				root.appendChild(rotateField);
			}
			if (this.move != 0){
				Element moveField = doc.createElement("move");
				moveField.setAttribute("req",Integer.toString(this.move));
				root.appendChild(moveField);
			}

			if (this.numScans != 0){
				Element scanField = doc.createElement("scan");
				scanField.setAttribute("req", Integer.toString(this.numScans));
				root.appendChild(scanField);
			}
			
			if (this.reboot){
				Element rebootField = doc.createElement("reboot");
				root.appendChild(rebootField);
			}
			
			if (this.D != null && this.D != Drive.UNCHANGED){
				Element driveField = doc.createElement("G4Drive");
				if (this.D == Drive.FORWARD)
					driveField.setAttribute("drive", "1");
				if (this.D == Drive.BACKWARD)
					driveField.setAttribute("drive", "-1");
				if (this.D == Drive.STOP)
					driveField.setAttribute("drive", "0");
				root.appendChild(driveField);
			}

			if (this.T != null && this.T != Turn.UNCHANGED){
				Element turnField = doc.createElement("G4Turn");
				if (this.T == Turn.LEFT)
					turnField.setAttribute("turn", "-1");
				if (this.T == Turn.RIGHT)
					turnField.setAttribute("turn", "1");
				if (this.T == Turn.STOP)
					turnField.setAttribute("turn", "0");
				root.appendChild(turnField);
			}
			
			TransformerFactory tFact = TransformerFactory.newInstance();
			Transformer trans = tFact.newTransformer();

			StringWriter writer = new StringWriter();
			StreamResult result = new StreamResult(writer);
			DOMSource source = new DOMSource(doc);
			trans.transform(source, result);
			return writer.toString();
		} catch (TransformerException ex) {
			System.out.println("Error outputting document");
		} catch (ParserConfigurationException ex) {
			System.out.println("Error building document");
		}
		return "";
	}
	public String toString(){
		return getXML();
	}
}
